$(function () {
    var $title = $("#title");
    $title.keydown(function (e) {
        var e = e || event;
        if (e.preventDefault) {
            e.preventDefault();
          }
        if (e.keyCode === 13) {
            var value = $title.val().replace(/(^\s*)|(\s*$)/g, "");
            if (value === "") {
                return;
            } else {
                createTodo();
                $title.val("");
            }

        }
    });
    function createTodo() {
        var val = $title.val();

        localStorage.setItem(val, val);
        var $li = $(` 
                    <li>
                        <input type="checkbox">
                        <p>${val}</p>
                        <a href="javascript:void(0)">-</a> 
                    </li>`);
        $("#todolist").append($li);
        counts();
    }
    $("section").on("change", "input[type=checkbox]", function () {

        if ($(this).prop("checked")) {
            $("#donelist").append($(this).parent());
            counts();
        } else {
            $("#todolist").append($(this).parent());
            counts();
        }
    });
    $("#todolist").on("click", "p", function () { 
        $(this).html("<input type='text' value='" + $(this).text() + "'>");
        var $ipt = $(this).children();
        $ipt.click(function (e) {
            var e = e || event;
            if (e.preventDefault) {
                e.preventDefault();
              }
            e.stopPropagation();
        })
        $ipt.select(); 
        $ipt.blur(function (e) {
            this.outerHTML = this.value;
        })
    })
    $("section").on("click", "a", function () {
        $(this).parent().remove()
        var name = $(this).prev().html();
        localStorage.removeItem(name);
        counts();
    })
    function counts() {
        var $count1 = $("#todolist").children().length;
        var $count2 = $("#donelist").children().length;
        $("#todocount").html($count1);
        $("#donecount").html($count2);
    }
    counts();

    $(window).on("click", function () {
        // e.preventDefault()
        $.ajax({
            type: "GET",
            url: "https://api.i‐lynn.cn/getIpInfo",
            data: "",
            success: function (res) {
                console.log(res);
                fn(res)
            }
        })
    })
    function fn(res) {
        $("#ipv4").html(`${res.ip}`)
        $("#addr").html(`${res.country} ${res.area}互联网数据交换中心`)
    }
})